# Copyright (c) 2001 IBK-Landquart-Switzerland. All rights reserved.
#
# Module      :  makefile
# Application :  IBK Open Class Library
# Purpose     :  Build all targets with GCC
#
# Date        Description                                 Who
# --------------------------------------------------------------------------
# 2001.06.18  First implemention	                  P.Koch, IBK
# 2001.07.11  Rename to CSL                               P.Koch, IBK
# 2001.10.27  v4.3.0                                      P.Koch, IBK
#
# OPEN SOURCE LICENSE
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to IBK at info@ibk-software.ch.

#############################################################################
#                 C O N F I G U R A T I O N   S E C T I O N                 #
#############################################################################

# Uncomment a pair of lines for MYSQLLIB and MYSQLINCS if you want to build
# the MySQL interface. Below are some typical samples, choose the pair
# reflecting your MySQL installation or create your own.

#MYSQLLIB   = /usr/local/mysql/lib/libmysqlclient.a
#MYSQLINCS  = /usr/local/mysql/include

#MYSQLLIB   = /usr/local/lib/mysql/libmysqlclient.so
#MYSQLINCS  = /usr/local/include/mysql

#MYSQLLIB   = /usr/lib/mysql/libmysqlclient.a
#MYSQLINCS  = /usr/include/mysql

#############################################################################
#   N O   C H A N G E S   B E L O W   T H I S   P O I N T ,  P L E A S E    #
#############################################################################

MAJORVERS  = 4
MINORVERS  = 3
REVISION   = 0

PREFIX     ?= /usr/local

BINDIR     = $(PREFIX)/bin
LIBDIR     = $(PREFIX)/lib
INCDIR     = $(PREFIX)/include
CSLDIR     = $(PREFIX)/share/csl
SMPDIR     = $(PREFIX)/share/csl/samples
DOCDIR     = $(PREFIX)/share/doc/csl

ROOTDIR    = ../../..
SRCROOT    = $(ROOTDIR)/Source
OPSYSTEM   = $(shell uname -s)

export MAJORVERS
export MINORVERS
export REVISION
export PREFIX
export INCDIR
export CSLDIR
export SMPDIR
export LIBDIR
export BINDIR
export ROOTDIR
export SRCROOT
export OPSYSTEM

TARGETS = \
  Base.bld \
  Csl.bld \
  CslApi.bld \
  CslWrap.bld \
  ComLib.bld \
  DaxLib.bld \
  FileLb.bld \
  MathLb.bld \
  RexLib.bld \
  SysLib.bld \
  StrLib.bld \
  CslExe.bld

ifdef MYSQLLIB
  TARGETS += Mysql.bld
  export MYSQLLIB
  export MYSQLINCS
endif

#############################################################################

.PHONY : all

all : $(TARGETS)

$(TARGETS):
	@echo --------------------------------------------------------------
	@echo building $(basename $@)
	@$(MAKE) -C $(basename $@) 

#############################################################################

.PHONY : clean

clean : $(TARGETS:.bld=.cln)
	@rm -rf *~

$(TARGETS:.bld=.cln) :
	@echo cleaning up $(basename $@)
	@$(MAKE) -C $(basename $@)  clean

#############################################################################

.PHONY : install

TARGETDIRS = \
  $(INCDIR) \
  $(CSLDIR) \
  $(LIBDIR) \
  $(BINDIR) \
  $(SMPDIR) \
  $(DOCDIR)/html \
  $(DOCDIR)/img

$(TARGETDIRS) :
	@echo make directory $@
	@mkdir -p $@

install : all  sepline $(TARGETDIRS) $(TARGETS:.bld=.itl)
	@echo installing documentation and samples
	@cp -f ../../csldoc/*.* $(DOCDIR)/html
	@cp -f ../../img/*.* $(DOCDIR)/img
	@cp -Rf ../../Samples/* $(SMPDIR)
ifeq "$(prefix)" "/usr"
	@echo running ldconfig
	@ldconfig
endif
ifeq "$(prefix)" "/usr/local"
	@echo running ldconfig
	@ldconfig
endif

$(TARGETS:.bld=.itl) :
	@echo installing $(basename $@)
	@$(MAKE) -C $(basename $@) install

#############################################################################

.PHONY : uninstall

uninstall :
	@echo uninstalling program
	@rm -f $(BINDIR)/csl $(BINDIR)/csl.*
	@echo uninstalling libraries
	@rm -f $(LIBDIR)/libZBaseG.*
	@rm -f $(LIBDIR)/libZCslApi.*
	@rm -f $(LIBDIR)/libZCslG.*
	@rm -f $(LIBDIR)/libZCslWrpG.*
	@rm -f $(LIBDIR)/libZMysqlG.*
	@rm -f $(LIBDIR)/libZcComLib.*
	@rm -f $(LIBDIR)/libZcDaxLib.*
	@rm -f $(LIBDIR)/libZcFileLb.*
	@rm -f $(LIBDIR)/libZcMathLb.*
	@rm -f $(LIBDIR)/libZcRexLib.*
	@rm -f $(LIBDIR)/libZcStrLib.*
	@rm -f $(LIBDIR)/libZcSysLib.*
	@echo uninstalling include files
	@rm -f $(INCDIR)/ZAsync.h*
	@rm -f $(INCDIR)/ZBase.h*
	@rm -f $(INCDIR)/ZCsl.h*
	@rm -f $(INCDIR)/ZCslApi.h*
	@rm -f $(INCDIR)/ZCslWrap.h*
	@rm -f $(INCDIR)/ZCss.h*
	@rm -f $(INCDIR)/ZCssApi.h*
	@rm -f $(INCDIR)/ZCssWrap.h*
	@rm -f $(INCDIR)/ZDate.h*
	@rm -f $(INCDIR)/ZDynLink.h*
	@rm -f $(INCDIR)/ZExcept.h*
	@rm -f $(INCDIR)/ZFile.h*
	@rm -f $(INCDIR)/ZMysql.h*
	@rm -f $(INCDIR)/ZPlatfrm.h*
	@rm -f $(INCDIR)/ZProcess.h*
	@rm -f $(INCDIR)/ZProfile.h*
	@rm -f $(INCDIR)/ZPtrlist.h*
	@rm -f $(INCDIR)/ZRegular.h*
	@rm -f $(INCDIR)/ZSql.h*
	@rm -f $(INCDIR)/ZString.h*
	@rm -f $(INCDIR)/ZStrlist.h*
	@rm -f $(INCDIR)/ZTrace.h*
	@echo uninstalling scripts
	@rm -f $(CSLDIR)/ZcComLib.csl
	@rm -f $(CSLDIR)/ZcDaxLib.csl
	@rm -f $(CSLDIR)/ZcFileLb.csl
	@rm -f $(CSLDIR)/ZcMathLb.csl
	@rm -f $(CSLDIR)/ZcRexLib.csl
	@rm -f $(CSLDIR)/ZcStrLib.csl
	@rm -f $(CSLDIR)/ZcSysLib.csl
	@echo uninstalling documentation and samples
	@rm -rf $(DOCDIR)/html
	@rm -rf $(DOCDIR)/img
	@rm -rf $(SMPDIR)
	@echo trying to remove emty directories \(there may be warnings\)
	-@rmdir $(BINDIR)
	-@rmdir $(LIBDIR)
	-@rmdir $(INCDIR)
	-@rmdir $(CSLDIR)
	-@rmdir $(DOCDIR)

#############################################################################

.PHONY: dist

CSLARCH=csl-$(MAJORVERS).$(MINORVERS).$(REVISION)
CSLROOT=../..

dist:
	@echo create directory structure
	@rm -rf $(CSLARCH)
	@mkdir -p $(CSLARCH)/Source/Base
	@mkdir -p $(CSLARCH)/Source/Csl
	@mkdir -p $(CSLARCH)/Source/CslApi
	@mkdir -p $(CSLARCH)/Source/CslWrap
	@mkdir -p $(CSLARCH)/Source/CslExe
	@mkdir -p $(CSLARCH)/Source/Oracle
	@mkdir -p $(CSLARCH)/Source/Db2
	@mkdir -p $(CSLARCH)/Source/Mysql
	@mkdir -p $(CSLARCH)/Source/Odbc
	@mkdir -p $(CSLARCH)/Source/ComLib
	@mkdir -p $(CSLARCH)/Source/DaxLib
	@mkdir -p $(CSLARCH)/Source/FileLb
	@mkdir -p $(CSLARCH)/Source/MathLb
	@mkdir -p $(CSLARCH)/Source/RexLib
	@mkdir -p $(CSLARCH)/Source/StrLib
	@mkdir -p $(CSLARCH)/Source/SysLib
	@mkdir -p $(CSLARCH)/Unix/Gnu/Base
	@mkdir -p $(CSLARCH)/Unix/Gnu/ComLib
	@mkdir -p $(CSLARCH)/Unix/Gnu/Csl
	@mkdir -p $(CSLARCH)/Unix/Gnu/CslApi
	@mkdir -p $(CSLARCH)/Unix/Gnu/CslExe
	@mkdir -p $(CSLARCH)/Unix/Gnu/CslWrap
	@mkdir -p $(CSLARCH)/Unix/Gnu/DaxLib
	@mkdir -p $(CSLARCH)/Unix/Gnu/FileLb
	@mkdir -p $(CSLARCH)/Unix/Gnu/MathLb
	@mkdir -p $(CSLARCH)/Unix/Gnu/Mysql
	@mkdir -p $(CSLARCH)/Unix/Gnu/RexLib
	@mkdir -p $(CSLARCH)/Unix/Gnu/StrLib
	@mkdir -p $(CSLARCH)/Unix/Gnu/SysLib
	@mkdir -p $(CSLARCH)/Samples/Api/Source
	@mkdir -p $(CSLARCH)/Samples/Api/Unix/Gnu
	@mkdir -p $(CSLARCH)/Samples/Class/Source
	@mkdir -p $(CSLARCH)/Samples/Class/Unix/Gnu
	@mkdir -p $(CSLARCH)/csldoc
	@mkdir -p $(CSLARCH)/img
	@echo copy files
	@cp $(CSLROOT)/README                            $(CSLARCH)
	@cp $(CSLROOT)/LICENSE                           $(CSLARCH)
	@cp $(CSLROOT)/Source/Base/ZAsync.cpp            $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZAsync.hpp            $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZBase.h               $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZBase.hpp             $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZDate.cpp             $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZDate.hpp             $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZDynLink.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZDynLink.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZExcept.cpp           $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZExcept.hpp           $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZFile.cpp             $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZFile.hpp             $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZPlatfrm.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZPlatfrm.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZProcess.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZProcess.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZProfile.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZProfile.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZPtrlist.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZPtrlist.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZRegular.cpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZRegular.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZSql.cpp              $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZSql.hpp              $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZString.cpp           $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZString.hpp           $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZStrlist.hpp          $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZTrace.cpp            $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Base/ZTrace.hpp            $(CSLARCH)/Source/Base
	@cp $(CSLROOT)/Source/Csl/Block.cpp              $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Compile.cpp            $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Function.cpp           $(CSLARCH)/Source/Csl
# Added by CL-team members:
#      jean-luc.amitousa-mankoy@hotmail.fr
#      boris.de.finance@gmail.com
	@cp $(CSLROOT)/Source/Csl/SavedContinuations.cpp $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/TraceEnvs.cpp $(CSLARCH)/Source/Csl
########################################################################## 
	@cp $(CSLROOT)/Source/Csl/Input.cpp              $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Instruct.cpp           $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Internal.h             $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Interprt.cpp           $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Library.cpp            $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Parametr.cpp           $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Utility.cpp            $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/Variable.cpp           $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/ZCsl.cpp               $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/ZCsl.h                 $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/ZCsl.hpp               $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/Csl/ZCss.hpp               $(CSLARCH)/Source/Csl
	@cp $(CSLROOT)/Source/CslApi/ZCslApi.cpp         $(CSLARCH)/Source/CslApi
	@cp $(CSLROOT)/Source/CslApi/ZCslApi.h           $(CSLARCH)/Source/CslApi
	@cp $(CSLROOT)/Source/CslApi/ZCssApi.h           $(CSLARCH)/Source/CslApi
	@cp $(CSLROOT)/Source/CslWrap/ZCslWrap.cpp       $(CSLARCH)/Source/CslWrap
	@cp $(CSLROOT)/Source/CslWrap/ZCslWrap.hpp       $(CSLARCH)/Source/CslWrap
	@cp $(CSLROOT)/Source/CslWrap/ZCssWrap.hpp       $(CSLARCH)/Source/CslWrap
	@cp $(CSLROOT)/Source/CslExe/Csl.cpp             $(CSLARCH)/Source/CslExe
	@cp $(CSLROOT)/Source/Oracle/ZOracle.cpp         $(CSLARCH)/Source/Oracle
	@cp $(CSLROOT)/Source/Oracle/ZOracle.hpp         $(CSLARCH)/Source/Oracle
	@cp $(CSLROOT)/Source/Db2/ZDb2.cpp               $(CSLARCH)/Source/Db2
	@cp $(CSLROOT)/Source/Db2/ZDb2.hpp               $(CSLARCH)/Source/Db2
	@cp $(CSLROOT)/Source/Mysql/ZMysql.cpp           $(CSLARCH)/Source/Mysql
	@cp $(CSLROOT)/Source/Mysql/ZMysql.hpp           $(CSLARCH)/Source/Mysql
	@cp $(CSLROOT)/Source/Odbc/ZOdbc.cpp             $(CSLARCH)/Source/Odbc
	@cp $(CSLROOT)/Source/Odbc/ZOdbc.hpp             $(CSLARCH)/Source/Odbc
	@cp $(CSLROOT)/Source/ComLib/ZcComLib.cpp        $(CSLARCH)/Source/ComLib
	@cp $(CSLROOT)/Source/ComLib/ZcComLib.csl        $(CSLARCH)/Source/ComLib
	@cp $(CSLROOT)/Source/DaxLib/ZcDaxLib.cpp        $(CSLARCH)/Source/DaxLib
	@cp $(CSLROOT)/Source/DaxLib/ZcDaxLib.csl        $(CSLARCH)/Source/DaxLib
	@cp $(CSLROOT)/Source/FileLb/ZcFileLb.cpp        $(CSLARCH)/Source/FileLb
	@cp $(CSLROOT)/Source/FileLb/ZcFileLb.csl        $(CSLARCH)/Source/FileLb
	@cp $(CSLROOT)/Source/MathLb/ZcMathLb.cpp        $(CSLARCH)/Source/MathLb
	@cp $(CSLROOT)/Source/MathLb/ZcMathLb.csl        $(CSLARCH)/Source/MathLb
	@cp $(CSLROOT)/Source/RexLib/ZcRexLib.cpp        $(CSLARCH)/Source/RexLib
	@cp $(CSLROOT)/Source/RexLib/ZcRexLib.csl        $(CSLARCH)/Source/RexLib
	@cp $(CSLROOT)/Source/StrLib/ZcStrLib.cpp        $(CSLARCH)/Source/StrLib
	@cp $(CSLROOT)/Source/StrLib/ZcStrLib.csl        $(CSLARCH)/Source/StrLib
	@cp $(CSLROOT)/Source/SysLib/ZcSysLib.cpp        $(CSLARCH)/Source/SysLib
	@cp $(CSLROOT)/Source/SysLib/ZcSysLib.csl        $(CSLARCH)/Source/SysLib
	@cp INSTALL                                      $(CSLARCH)/Unix/Gnu
	@cp makefile                                     $(CSLARCH)/Unix/Gnu
	@cp makelib                                      $(CSLARCH)/Unix/Gnu
	@cp makeprog                                     $(CSLARCH)/Unix/Gnu
	@cp mkcsllib                                     $(CSLARCH)/Unix/Gnu
	@cp Base/makefile                                $(CSLARCH)/Unix/Gnu/Base
	@cp ComLib/makefile                              $(CSLARCH)/Unix/Gnu/ComLib
	@cp Csl/makefile                                 $(CSLARCH)/Unix/Gnu/Csl
	@cp CslApi/makefile                              $(CSLARCH)/Unix/Gnu/CslApi
	@cp CslExe/makefile                              $(CSLARCH)/Unix/Gnu/CslExe
	@cp CslWrap/makefile                             $(CSLARCH)/Unix/Gnu/CslWrap
	@cp DaxLib/makefile                              $(CSLARCH)/Unix/Gnu/DaxLib
	@cp FileLb/makefile                              $(CSLARCH)/Unix/Gnu/FileLb
	@cp MathLb/makefile                              $(CSLARCH)/Unix/Gnu/MathLb
	@cp Mysql/makefile                               $(CSLARCH)/Unix/Gnu/Mysql
	@cp RexLib/makefile                              $(CSLARCH)/Unix/Gnu/RexLib
	@cp StrLib/makefile                              $(CSLARCH)/Unix/Gnu/StrLib
	@cp SysLib/makefile                              $(CSLARCH)/Unix/Gnu/SysLib
	@cp $(CSLROOT)/Samples/args.csl                  $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/cgiform.htm               $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/cgiprog                   $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/cgiread.txt               $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/hello.csl                 $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/portable.csl              $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/regular.csl               $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/toys.csl                  $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/trace.csl                 $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/unknown.csl               $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/tss                       $(CSLARCH)/Samples
	@cp $(CSLROOT)/Samples/Api/Source/Embed.c        $(CSLARCH)/Samples/Api/Source
	@cp $(CSLROOT)/Samples/Api/Source/MyLib.c        $(CSLARCH)/Samples/Api/Source
	@cp $(CSLROOT)/Samples/Api/Source/testlib.csl    $(CSLARCH)/Samples/Api/Source
	@cp $(CSLROOT)/Samples/Api/Unix/Gnu/build        $(CSLARCH)/Samples/Api/Unix/Gnu
	@cp $(CSLROOT)/Samples/Api/Unix/Gnu/cleanup      $(CSLARCH)/Samples/Api/Unix/Gnu
	@cp $(CSLROOT)/Samples/Class/Source/Embed.cpp    $(CSLARCH)/Samples/Class/Source
	@cp $(CSLROOT)/Samples/Class/Source/MyLib.cpp    $(CSLARCH)/Samples/Class/Source
	@cp $(CSLROOT)/Samples/Class/Source/testlib.csl  $(CSLARCH)/Samples/Class/Source
	@cp $(CSLROOT)/Samples/Class/Unix/Gnu/build      $(CSLARCH)/Samples/Class/Unix/Gnu
	@cp $(CSLROOT)/Samples/Class/Unix/Gnu/cleanup    $(CSLARCH)/Samples/Class/Unix/Gnu
	@cp $(CSLROOT)/csldoc/*.htm                      $(CSLARCH)/csldoc
	@cp $(CSLROOT)/csldoc/*.html                     $(CSLARCH)/csldoc
	@cp $(CSLROOT)/img/*.png                         $(CSLARCH)/img
	@echo create archive $(CSLARCH).tar.gz
	@tar -czf $(CSLARCH).tar.gz $(CSLARCH)
	@rm -rf $(CSLARCH)

#############################################################################

.PHONY: distbinary

BINARCH=csl-$(MAJORVERS).$(MINORVERS).$(REVISION)-$(shell uname -s)-$(shell uname -m)

distbinary:
	@echo create binary distribution
	@rm -rf distbinary
	@rm -rf $(BINARCH)
	@mkdir distbinary
	@mkdir $(BINARCH)
	@$(MAKE)  install PREFIX=$(shell pwd)/distbinary
	@echo create archive files.tar
	@tar -C distbinary -cf $(BINARCH)/files.tar bin lib include share
	@rm -rf distbinary
	@cp $(CSLROOT)/README  $(BINARCH)
	@cp $(CSLROOT)/LICENSE $(BINARCH)
	@echo create archive $(BINARCH).tar.gz
	@tar -czf $(BINARCH).tar.gz $(BINARCH)
	@rm -rf $(BINARCH)

#############################################################################

.PHONY: sepline

sepline:
	@echo --------------------------------------------------------------

